I'm a beginner in R and I've been trying to scrape the following website:
to automatically start downloading an excel file from this air monitoring station here in Chile. However, I noticed that using SelectorGadget the download link couldn't be selected and found out it was because the link was a javascript link (href="javascript:Open('xcl');">Excel CSV) as seen in the image below.
My question is there any way I can simulate a click on that javascript link? I've already simulated clicks for other parts of the same website succesfully but they where not javascript links. This is what I've already tried:
library(rvest)
library(RSelenium)
rD <- rsDriver(browser="firefox", port=4040L, verbose=F)
remDr <- rD[["client"]]
remDr$open()
remDr$navigate("https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes¯opath=./RM/D13/Cal/0001¯o=0001.diario.diario&from=970508&to=090312&")
Sys.sleep(5) # give the page time to fully load
# click on excel link to start downloading file (doesnt work) ------------
xls <- remDr$findElement("xpath", "/html/body/table/tbody/tr/td/table[2]/tbody/tr[1]/td/label/span[3]/a")
xls$clickElement()
/html/body/table/tbody/tr/td/table[2]/tbody/tr[1]/td/label/span[3]/a
##### TESTING
remDr$navigate("https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes¯opath=./RM/D13/Cal/0001¯o=0001.diario.diario&from=970508&to=090312&")
excel <- remDr$findElement(using = "css selector", "[href*=javascript]")
#excel <- remDr$findElement(using = "css selector", '[href*="javascript:Open('xcl')"]')
excel$highlightElement() # to visually check what elemnet is selected
remDr$closeServer()
remDr$close()
My objective is to be able to click the link and start downloading the excel file. I've been trying to do this for a couple days and I'm stuck, so any help would be greatly appreciated.
It is inside a frame that is inside a frameset. You first need to switch to that frame via the parent frameset, using RSelenium. You can use the frame ids frMain and right to target the elements to switch to. Then you should be able to simply target that href by using a css attribute = value selector of [href*=xcl]. The * is the contains operator specifying that the href attribute value of the target contains the substring xcl.
Something like the following (not tested):
frame_set <- remDr$findElement("css", "#frMain")
remDr$switchToFrame(frame_set)
child_frame <- remDr$findElement("css", "#right")
remDr$switchToFrame(child_frame)
csv_download <- remDr$findElement(using = "css", "[href*=xcl]")
csv_download$clickElement()
You can use browser dev tools F12 to view that path: